package com.sheepone.distributor;

import com.sheepone.distributor.test.StationConstants;
import com.sheepone.distributor.test.StringContext;
import com.sheepone.distributor.test.TopicConstants;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.LongAdder;

@SpringBootApplication
public class DistributorApplication {
    public static LongAdder read = new LongAdder();
    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(DistributorApplication.class, args);
        Distributor distributor = applicationContext.getBean(Distributor.class);
        long num = 18000;
        new Thread(()->{
            System.out.println("开始发送消息");
            for (int i=0;i<10;i++){
                distributor.distributeDelay(StationConstants.DELAY, TopicConstants.DELAY,new StringContext("Delay"+i),System.currentTimeMillis()+3000);
            }
            for (int i=10;i<num;i++){
                distributor.distribute(StationConstants.ADD, TopicConstants.ADD,new StringContext("hello"+i));
            }
        }).start();
        long start = System.nanoTime();
        while(read.longValue() < num){
            Thread.onSpinWait();
        }
        long duration = System.nanoTime() - start;
        System.out.println(duration+"纳秒");
        System.out.println(TimeUnit.NANOSECONDS.toMillis(duration) +"毫秒");
        System.out.println(TimeUnit.NANOSECONDS.toSeconds(duration) +"秒");
        System.out.println(read.longValue());
        distributor.stop();
    }
}
